<template>
  <div class="task-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>任务管理</span>
          <el-button type="primary" @click="showCreateDialog">创建任务</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm">
        <el-form-item label="类型">
          <el-input v-model="queryForm.taskType" placeholder="任务类型" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="状态" clearable>
            <el-option label="待处理" value="PENDING" />
            <el-option label="执行中" value="RUNNING" />
            <el-option label="成功" value="SUCCESS" />
            <el-option label="失败" value="FAILED" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadData">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="taskId" label="任务ID" width="200" />
        <el-table-column prop="taskName" label="名称" width="150" />
        <el-table-column prop="taskType" label="类型" width="120" />
        <el-table-column prop="priority" label="优先级" width="80" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ getStatusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="assignedAgentId" label="分配智能体" width="200" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="viewDetail(row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="pagination.current"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        @current-change="loadData"
        layout="total, prev, pager, next"
        style="margin-top: 20px; justify-content: center"
      />
    </el-card>

    <el-dialog v-model="createDialogVisible" title="创建任务" width="500px">
      <el-form :model="createForm" :rules="createRules" ref="createFormRef" label-width="100px">
        <el-form-item label="名称" prop="taskName">
          <el-input v-model="createForm.taskName" />
        </el-form-item>
        <el-form-item label="类型" prop="taskType">
          <el-input v-model="createForm.taskType" />
        </el-form-item>
        <el-form-item label="优先级">
          <el-input-number v-model="createForm.priority" :min="0" :max="10" />
        </el-form-item>
        <el-form-item label="输入数据">
          <el-input v-model="createForm.inputData" type="textarea" :rows="4" placeholder='{"key": "value"}' />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="createDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确定</el-button>
      </template>
    </el-dialog>

    <el-dialog v-model="detailDialogVisible" title="任务详情" width="700px">
      <el-descriptions :column="2" border v-if="currentTask">
        <el-descriptions-item label="任务ID" :span="2">{{ currentTask.taskId }}</el-descriptions-item>
        <el-descriptions-item label="名称">{{ currentTask.taskName }}</el-descriptions-item>
        <el-descriptions-item label="类型">{{ currentTask.taskType }}</el-descriptions-item>
        <el-descriptions-item label="优先级">{{ currentTask.priority }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="getStatusType(currentTask.status)">{{ getStatusText(currentTask.status) }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="分配智能体" :span="2">{{ currentTask.assignedAgentId || '未分配' }}</el-descriptions-item>
        <el-descriptions-item label="输入数据" :span="2">
          <pre>{{ currentTask.inputData }}</pre>
        </el-descriptions-item>
        <el-descriptions-item label="输出数据" :span="2">
          <pre>{{ currentTask.outputData || '无' }}</pre>
        </el-descriptions-item>
        <el-descriptions-item label="错误信息" :span="2" v-if="currentTask.errorMessage">
          <el-text type="danger">{{ currentTask.errorMessage }}</el-text>
        </el-descriptions-item>
        <el-descriptions-item label="开始时间">{{ currentTask.startTime || '未开始' }}</el-descriptions-item>
        <el-descriptions-item label="结束时间">{{ currentTask.endTime || '未结束' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间" :span="2">{{ currentTask.createTime }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getTaskList, createTask, getTaskDetail } from '@/api/task'

const tableData = ref([])
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

const queryForm = reactive({
  taskType: '',
  status: ''
})

const createDialogVisible = ref(false)
const detailDialogVisible = ref(false)
const currentTask = ref(null)

const createForm = reactive({
  taskName: '',
  taskType: '',
  priority: 0,
  inputData: ''
})

const createRules = {
  taskName: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
  taskType: [{ required: true, message: '请输入任务类型', trigger: 'blur' }]
}

const createFormRef = ref(null)

const loadData = async () => {
  const params = {
    current: pagination.current,
    size: pagination.size,
    ...queryForm
  }
  const res = await getTaskList(params)
  tableData.value = res.data.records
  pagination.total = res.data.total
}

const resetQuery = () => {
  queryForm.taskType = ''
  queryForm.status = ''
  pagination.current = 1
  loadData()
}

const showCreateDialog = () => {
  createDialogVisible.value = true
}

const handleCreate = async () => {
  await createFormRef.value.validate()
  await createTask(createForm)
  ElMessage.success('创建成功')
  createDialogVisible.value = false
  Object.assign(createForm, {
    taskName: '',
    taskType: '',
    priority: 0,
    inputData: ''
  })
  loadData()
}

const viewDetail = async (row) => {
  const res = await getTaskDetail(row.taskId)
  currentTask.value = res.data
  detailDialogVisible.value = true
}

const getStatusType = (status) => {
  const map = {
    PENDING: 'info',
    RUNNING: 'warning',
    SUCCESS: 'success',
    FAILED: 'danger'
  }
  return map[status] || 'info'
}

const getStatusText = (status) => {
  const map = {
    PENDING: '待处理',
    RUNNING: '执行中',
    SUCCESS: '成功',
    FAILED: '失败'
  }
  return map[status] || status
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.task-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

pre {
  background: #f5f5f5;
  padding: 10px;
  border-radius: 4px;
  max-height: 200px;
  overflow: auto;
}
</style>
